import {createStoreBindings} from 'mobx-miniprogram-bindings'
import {
    store
} from '../store/store'
module.exports = Behavior({
    observers:{
        'total':function(newVal) {
            this.setBadge()
            if (newVal === 0) wx.hideTabBarRedDot({
              index: 2
            })
          },

    },
    methods:{
        setBadge() {
            // 调用 uni.setTabBarBadge() 方法，为购物车设置右上角的徽标
            if(this.data.total===0){
                wx.removeTabBarBadge({
                  index: 2,
                }).catch(err=>err)
                return
            }
            wx.setTabBarBadge({
              index: 2,
              text: this.data.total + '', // 注意：text 的值必须是字符串，不能是数字
            }).catch(err=>err)
          },
    },
    lifetimes:{
        attached(){
            // 加载
            this.storeBindings = createStoreBindings(this, {
                store,
                fields: ['cart','total','token','user','redirectInfo','address'],
                actions: ['addToCart','saveToStorage','changeGoodsState','changeGoodsCount','removeGoodsById','changeAllGoodsState','updateToken','updateUser','updateRedirectInfo','updateAddress']
              })
        },
        detached(){
            this.storeBindings.destroyStoreBindings()
        }
    }
   
    
})